/*
 Copyright (C) 2009 QDSS.org
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.qdss.persist.engine.query;

import java.util.LinkedList;
import java.util.List;


import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.qdss.persist.Criteria;
import org.qdss.persist.Entity;
import org.qdss.persist.Filter;
import org.qdss.persist.PersistManagerFactory;
import org.qdss.persist.Query;
import org.qdss.persist.Record;
import org.qdss.persist.engine.entry.ID;
import org.qdss.persist.sql.compiler.SelectItem;

/**
 * 
 * 
 * @author <a href="mailto:zhaofang123@gmail.com">FANGFANG ZHAO</a>
 * @see 0.1.0, Apr 7, 2009
 * @version $Id: CriteriaImpl.java 49 2009-04-24 16:42:55Z zhaofang123@gmail.com $
 */
public class CriteriaImpl implements Criteria {
	private static final long serialVersionUID = 1353329773883682140L;

	private Entity entity;

	private List<String> selectItems = new LinkedList<String>();
	private ID primaryId;
	private String whereClause;

	private String orderBy;
	private boolean ascending = true;

	private String groupBy;
	

	public CriteriaImpl(Entity entity, PersistManagerFactory managerFactory) {
//		super(null, managerFactory);
		this.entity = entity;
	}

	public Criteria select(String item) {
		selectItems.add(item);
		return this;
	}

	public Criteria select(String[] items) {
		CollectionUtils.addAll(selectItems, items);
		return this;
	}

	public Criteria setId(ID id) {
		this.primaryId = id;
		return this;
	}

	public Criteria where(String addToWhereClause) {
		this.whereClause = addToWhereClause;
		return this;
	}

	public Criteria setOrderBy(String field) {
		return setOrderBy(field, true);
	}

	public Criteria setOrderBy(String field, boolean ascending) {
		this.orderBy = field;
		this.ascending = ascending;
		return this;
	}

	public Criteria setGroupBy(String field) {
		this.groupBy = field;
		return this;
	}

	public String toAjql() {
		StringBuilder ql = new StringBuilder("select ");
		ql.append(StringUtils.join(selectItems.toArray(new String[selectItems.size()]), ", "));
		ql.append(" from ").append(entity.getName()).append(" where ( 1 = 1 )");

		if (primaryId != null)
			ql.append(" and ( ").append(entity.getPrimaryField().getName()).append(" = '").append(primaryId.toLiteral()).append("' )");
		if (whereClause != null)
			ql.append(" and ( ").append(whereClause).append(" )");
		
		if (orderBy != null)
			ql.append(" order by ").append(orderBy).append(ascending ? " asc" : " desc");
		if (groupBy != null)
			ql.append(" group by ").append(groupBy);
		
		return ql.toString();
	}
	
	
	// --------------------------------------------------------------------
	
	public Object[][] array() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public int getFirstResult() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	public int getMaxResults() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	public Entity getRootEntity() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public SelectItem[] getSelectItems() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public int getTimeout() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	public boolean isLazyLoad() {
		// TODO Auto-generated method stub
		return false;
	}
	
	public List<Record> list() {
		// TODO Auto-generated method stub
		return null;
	}
	public Record record() {
		// TODO Auto-generated method stub
		return null;
	}
	public Result result() {
		// TODO Auto-generated method stub
		return null;
	}
	public Query setFilter(Filter filter) {
		// TODO Auto-generated method stub
		return null;
	}
	public Query setFirstResult(int firstResult) {
		// TODO Auto-generated method stub
		return null;
	}
	public Query setMaxResults(int maxResults) {
		// TODO Auto-generated method stub
		return null;
	}
	public Query setParameter(int position, Object value) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public Query setParameter(String name, Object value) {
		// TODO Auto-generated method stub
		return null;
	}
	public Query setTimeout(int timeout) {
		// TODO Auto-generated method stub
		return null;
	}
	public Object[] unique() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public Query setLimit(int limit, int offset) {
		// TODO Auto-generated method stub
		return null;
	}
	
}
